package cn.datawin.task.pipe;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.datawin.spider.page.Page;
import cn.datawin.spider.pipeline.PipeLine;
import cn.datawin.spider.processor.Processor;
import cn.datawin.spider.util.UrlUtil;
import cn.datawin.task.Statistic;
import cn.datawin.task.Task;
import cn.datawin.task.util.HttpUtil;
import cn.datawin.task.util.TaskUtil;

public class PagePipeLine implements PipeLine{

	
	/**
	 * url
	 * page 规则 替换 
	 * ?page=
	 * pages
	 * 正则替换 或其他替换方式
	 * 
	 */
	@Override
	public void pipe(Processor process) {
		Task task = null;
		try {
			Page page = process.getPage();
			System.out.println("results======="+process.getParams());
			System.out.println("nextUrl======="+process.getList());
			System.out.println("===="+page.getHost());
			task=(Task)process.getTask();
			//判断url还是data  标识   解析方式判断是data  page  type
			String url="";
			List<Object> objects=process.getList();
			if(null==objects){
				return;
			}
			for (int i = 0; i < objects.size(); i++) {
				url=objects.get(i).toString();
				urlToTask(url,task,page);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				Statistic.getInstance().addCount(task);
			} catch (Exception e) {	}
		}
	}

	private void urlToTask(String url, Task task,Page page)throws Exception {
		if(url.startsWith("http")){
			task.setUrl(url);
		}else if(url.contains("javascript")){
			return;
		}else{
			task.setUrl(UrlUtil.canonicalizeUrl(url, page.getHttpRequest().getUrl()));
		}
		
		/*else if(url.startsWith("/")){
			task.setUrl(page.getHost()+url);
		}else{
			task.setUrl(page.getUri()+url);
		}*/
		List<Map<String, Object>> rules =task.getRules();
		task.setProcessor(task.nextProcessor());
		task.setWorkbeach(task.getWorkbeach());
		task.setClient(task.getClient());
		HashMap<String, String> obj = new HashMap<String, String>();
		obj.put("serializeList", HttpUtil.serialize(task));
		obj.put("rules", HttpUtil.serialize(rules));
		TaskUtil.addtask(obj);
		
	}

//	private void uToTask(String url, Task task,Page page,List<Rule> rules)throws Exception {
//		
//		/*
//		 * 
//				Map<String, Object> map=process.getList().get(i);
//				List<String> urls=(List<String>) map.get("urls");
//				List<Rule> rules=(List<Rule>) map.get("rules");
//				for (String u: urls) {
//					uToTask(u, task, page, rules);
//				}
//		 * 
//		 * */
//		if(url.startsWith("http://")){
//			task.setUrl(url);
//		}else if(url.startsWith("./")){
//			task.setUrl(page.getUri()+url);
//		}else if(url.contains("javascript")){
//			return;
//		}else{
//			task.setUrl(page.getHost()+url);
//		}
//		
//		task.setProcessor(rules.get(0).getType());
//		task.setWorkbeach(task.getWorkbeach());
//		task.setRules(rules);
//		TaskUtil.addtask(HttpUtil.serialize(task));
//	}
	
	
}
